package main;

import java.util.List;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Time;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import calcul.MonteCarlo;

import display.myFrame;

public class main {
	
	public static int NORMAL = 0;
	public static int THREADING = 1;
	
	private static List<Double> list1;
	private static List<Double> list2;
	private static List<Double> list3;
	private static List<Double> list4;
	private static List<Double> list5;
	

	public static List<Double> getList1() {
		return list1;
	}

	public static List<Double> getList2() {
		return list2;
	}

	public static List<Double> getList3() {
		return list3;
	}

	public static List<Double> getList4() {
		return list4;
	}

	public static List<Double> getList5() {
		return list5;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
            // Set System L&F
        /*try {
			UIManager.setLookAndFeel(
			UIManager.getSystemLookAndFeelClassName());
			
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
    
		readParameters();
		
		System.out.println("Begin");
		
		myFrame frame = new myFrame();
		frame.setVisible(true);	
		
	}
	
	public static HashMap<String, Object> calcul(int cPF, int threadingNB, double stock, double strike, double m, double dR, double cR, double v, Double double1, Double double2)
	{
		HashMap<String, Object> result = new HashMap<String, Object>();
		
		// time begin
		long t1 = System.currentTimeMillis();
		
		
		
		if (threadingNB == main.NORMAL)
		{
			MonteCarlo MC = new MonteCarlo(cPF, stock, strike, m, dR, cR, v, double1, double2);
			result.put("value", MC.calcul());
		}
		else
		{
			MonteCarlo MC = new MonteCarlo(cPF, stock, strike, m, dR, cR, v, double1, double2);
			result.put("value", MC.calculThread(threadingNB));
		}
		
		// time end
		long t2 = System.currentTimeMillis();
		
		result.put("time", getElapsedTimeHoursMinutesSecondsString(t2 - t1));
		
		return  result;
	}
	
	public static String getElapsedTimeHoursMinutesSecondsString(long elapsedTime) {  
		
	    String format = String.format("%%0%dd", 2);
	    
	    String mil = String.format(format, elapsedTime % 1000);  
	    elapsedTime = elapsedTime / 1000;  
	    
	    String seconds = String.format(format, elapsedTime % 60);  
	    String minutes = String.format(format, (elapsedTime % 3600) / 60);  
	    String hours = String.format(format, elapsedTime / 3600);  
	    String time =  hours + ":" + minutes + ":" + seconds + ":" + mil;  
	    return time;
	}  
	
	public static void readParameters()
	{
		Scanner scanner;
		
		try {
			
			scanner = new Scanner(new FileReader("data.txt"));
			
			ArrayList<Double> listAll = new ArrayList<Double>();
		       
	        while (scanner.hasNext()) {
	             listAll.add( scanner.nextDouble() );
	         }
	           
	        list1 = listAll.subList(0, 8);
	        list2 = listAll.subList(8, 16);
	        list3 = listAll.subList(16, 24);
	        list4 = listAll.subList(24, 32);
	        list5 = listAll.subList(32, 40);
	        
	        scanner.close();
	        
		} catch (FileNotFoundException e) {
			System.out.println("File NOT FOUND ! The file may be at the wrong place");
		}
	}
}
